# -*- coding:utf-8 -*-
# !/bin/sh
# !/usr/bin/python
# !/usr/bin/env

# import importlib #提供import语句
# import sys
# import time #提供延时功能
# import xlrd #excel文件读取
# import os
# import xlwt #excel文件写入
# from selenium.webdriver.common.by import By
#
# from xlutils.copy import copy #excel文件复制
# from selenium import webdriver #浏览器操作库
#
# importlib.reload(sys)
#
#
# #伪装成浏览器，防止被识破
# option = webdriver.ChromeOptions()
# option.add_argument('--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"')
# driver = webdriver.Chrome(options=option)
#
# #打开登录页面
# driver.get('https://www.qichacha.com/user_login')
# time.sleep(10)#等待20s，完成手动登录操作
# # 手动登录操作
#
#
# #从excel获取查询单位
# worksheet = xlrd.open_workbook(u'test.xls')
# sheet1 = worksheet.sheet_by_name("Sheet1")#excel有多个sheet，检索该名字的sheet表格
# rows = sheet1.nrows # 获取行数
# inc_list = []
# for i in range(1,rows) :
#     data = sheet1.cell_value(i, 1)  # 取第2列数据
#     inc_list.append(data)
# print(inc_list)
# inc_len = len(inc_list)
#
# #写回数据
# writesheet1 = copy(worksheet)# 这里复制了一个excel，没有直接写回最初的文件。
# # print(writesheet1)
# writesheet2 = writesheet1.get_sheet(0)#同样获得第一个sheet
# # print(writesheet2)
# style = xlwt.easyxf('font:height 240, color-index red, bold on;align: wrap on, vert centre, horiz center');
#
# #开启爬虫
# for i in range(inc_len):
#     txt = inc_list[i]
#     time.sleep(1)
#
#     if (i==0):
#         #向搜索框注入文字
#         driver.find_element(By.ID, 'searchKey').send_keys(txt)
#         #单击搜索按钮
#         driver.find_element(By.XPATH,'/html/body/div/div/section/div/div/div/div/div/div/span/button').click()
#     else:
#         #清楚搜索框内容
#         driver.find_element(By.ID,'searchKey').clear()
#         # 向搜索框注入下一个公司地址
#         driver.find_element(By.ID,'searchKey').send_keys(txt)
#         #搜索按钮
#         srh_btn = driver.find_element(By.XPATH,'/html/body/div/div/div/div/div/div/div/div/span/button')
#         srh_btn.click()
#
#     # 获取网页地址，进入
#     inner = driver.find_element(By.XPATH, '/html/body/div/div[2]/div[2]/div[4]/div/div[2]/div/table/tr[1]/td[3]/div/div[1]/span[1]/a').get_attribute("href")
#     print(inner)
#     driver.get(inner)
#     time.sleep(2)
#     # 弹出框按钮
#     try:
#         try:
#             srh_btn = driver.find_element(By.XPATH,'//*[@id="firstepdadModal"]/div/div/div[2]/button')
#             srh_btn.click()
#         except:
#             srh_btn = driver.find_element(By.XPATH,'//*[@id="firstcaseModal"]/div/div/div[2]/button')
#             srh_btn.click()
#     except:
#         pass
#     try:
#         # 转到企业发展
#         tag = driver.find_element(By.XPATH,'/html/body/div/div[2]/div[4]/div/div/div[1]/a[5]/h2')
#         tag.click()
#         time.sleep(2)
#         #获取首个企业信用码
#         try:
#             credit_code = driver.find_element(By.XPATH,'//*[@id="reportyear"]/div[2]/div/div[1]/div/table/tr[1]/td[2]').text
#         except:
#             credit_code='none'
#     except:
#         credit_code = 'none'
#
#
#     print(credit_code)
#     writesheet2.write(i+1, 15, credit_code)  # 第16列数据sheet1.write(i, j, data[j])
#     writesheet1.save(u'test2.xls')
#
# driver.close()


import sys
import re
import pymysql
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.Qt import *
from PyQt5.QtCore import QStringListModel


class Six(QWidget):
    def __init__(self):
        super().__init__()
        # 窗口标题
        self.setWindowTitle('iBox公告监控')
        # 窗口大小
        self.resize(720, 600)
        # 窗口居中
        self.center()
        # 图标
        self.text_browser = None
        # 二次添加
        self.table_view = None
        self.list_view = None
        self.content_browser = None
        self.key_label = None
        self.key_input = None
        self.send_label = None
        self.send_input = None
        self.pwd_label = None
        self.pwd_input = None
        self.rec_label = None
        self.rec_input = None
        self.rec_add_button = None
        self.delay_label = None
        self.delay_input = None
        self.start_button = None
        self.stop_button = None
        self.del_list = None
        self.init_ui()

    # 设置窗口主要部件和信号
    def init_ui(self):
        # 创建窗口
        self.create_window()
        self.set_list_view()
        self.set_content_browser()
        self.set_key_label()
        self.set_key_input()
        self.set_send_label()
        self.set_send_input()
        self.set_pwd_label()
        self.set_pwd_input()
        self.set_rec_label()
        self.set_rec_input()
        self.set_delay_label()
        self.set_delay_input()

        # TODO 绑定信号槽
        self.rec_add_button.clicked.connect(self.add_rec)

        # 右键菜单
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.list_view_menu)

    # 窗口居中
    def center(self):
        """
        窗口居中
        :return:
        """
        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move(round((screen.width() - size.width()) / 2), round((screen.height() - size.height()) / 2))

    # 窗口布局
    def create_window(self):
        # 输出内容
        self.content_browser = QTextBrowser()
        # 设置邮箱列表
        self.list_view = QListWidget()
        # 密钥文字
        self.key_label = QLabel()
        self.key_input = QLineEdit()
        # 发件人邮箱
        self.send_label = QLabel()
        self.send_input = QLineEdit()
        # 发件人密码
        self.pwd_label = QLabel()
        self.pwd_input = QLineEdit()
        # 收件人邮箱
        self.rec_label = QLabel()
        self.rec_input = QLineEdit()
        self.rec_add_button = QPushButton('添加')
        # 延迟
        self.delay_label = QLabel()
        self.delay_input = QLineEdit()
        self.start_button = QPushButton('启动')
        self.stop_button = QPushButton('停止')

        # 密钥
        key_layout = QHBoxLayout()
        key_layout.addWidget(self.key_label, 1)
        key_layout.addWidget(self.key_input, 3)

        # 发件人邮箱
        send_layout = QHBoxLayout()
        send_layout.addWidget(self.send_label, 1)
        send_layout.addWidget(self.send_input, 3)

        # 发件人密码
        pwd_layout = QHBoxLayout()
        pwd_layout.addWidget(self.pwd_label, 1)
        pwd_layout.addWidget(self.pwd_input, 3)

        # 收件人邮箱
        rec_right_layout = QHBoxLayout()
        rec_right_layout.addWidget(self.rec_input)
        rec_right_layout.addWidget(self.rec_add_button)
        rec_layout = QHBoxLayout()
        rec_layout.addWidget(self.rec_label, 1)
        rec_layout.addLayout(rec_right_layout, 3)

        # 延迟
        delay_right_layout = QHBoxLayout()
        delay_right_layout.addWidget(self.delay_input)
        delay_right_layout.addWidget(self.start_button)
        delay_right_layout.addWidget(self.stop_button)
        delay_layout = QHBoxLayout()
        delay_layout.addWidget(self.delay_label, 1)
        delay_layout.addLayout(delay_right_layout, 3)

        # 左下布局
        left_bottom_layout = QVBoxLayout()
        left_bottom_layout.addLayout(key_layout, 1)
        left_bottom_layout.addLayout(send_layout, 1)
        left_bottom_layout.addLayout(pwd_layout, 1)
        left_bottom_layout.addLayout(rec_layout, 1)
        left_bottom_layout.addLayout(delay_layout, 1)

        # 左侧布局
        left_layout = QVBoxLayout()
        left_layout.addWidget(self.list_view, 5)
        left_layout.addLayout(left_bottom_layout, 2)

        # 右侧布局
        right_layout = QVBoxLayout()
        right_layout.addWidget(self.content_browser)

        # 创建界面
        layout = QHBoxLayout(self)
        layout.addLayout(left_layout, 1)
        layout.addLayout(right_layout, 1)
        self.setLayout(layout)

    # TODO 输出内容
    def set_content_browser(self):
        self.content_browser.setPlaceholderText("""Any notice is issue...
2022-05-02 12:20:19
Any notice is issue...
2022-05-02 12:20:49
Any notice is issue...
2022-05-02 12:21:19
Any notice is issue...
2022-05-02 12:21:49
Any notice is issue...
2022-05-02 12:22:19
Any notice is issue...
2022-05-02 12:22:49
        """)

    # TODO 收件人邮箱列表
    def set_list_view(self):
        self.list_view.addItems(["2435406817@qq.com", "475731195@qq.com", "964919485@qq.com", "2016572814@qq.com",
                                 "2529165097@qq.com", "2227539211@qq.com", "2695413940@qq.com", "1007192355@qq.com",
                                 "2267305554@qq.com", "237292315@qq.com", "1922522242@qq.com", "1507882050@qq.com",
                                 "745584935@qq.com", "749242225@qq.com", "1390410876@qq.com", "1390410876@qq.com",
                                 "2084582383@qq.com"])

    def list_view_menu(self, pos):
        menu = QtWidgets.QMenu()
        opt1 = menu.addAction("删除")
        opt2 = menu.addAction("清空")
        action = menu.exec_(self.mapToGlobal(pos))

        if action == opt1:
            print(666)
        elif action == opt2:
            print(777)

    def del_list_action(self):
        pass

    # TODO 密钥
    def set_key_label(self):
        self.key_label.setText("密钥：")  # 创建一个QLabel并写入文字

    def set_key_input(self):
        # pass
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setFamily("Times New Roman")
        self.key_input.setFont(font)
        self.key_input.setTextMargins(5, 2, 0, 2)

    # TODO 发件人邮箱
    def set_send_label(self):
        self.send_label.setText("发件人邮箱：")  # 创建一个QLabel并写入文字

    def set_send_input(self):
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setFamily("Times New Roman")
        self.send_input.setFont(font)
        self.send_input.setTextMargins(5, 2, 0, 2)

    # TODO 发件人密码
    def set_pwd_label(self):
        self.pwd_label.setText("发件人授权码：")  # 创建一个QLabel并写入文字

    def set_pwd_input(self):
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setFamily("Times New Roman")
        self.pwd_input.setFont(font)
        self.pwd_input.setTextMargins(5, 2, 0, 2)

    # TODO 收件人邮箱
    def set_rec_label(self):
        self.rec_label.setText("收件人邮箱：")  # 创建一个QLabel并写入文字

    def set_rec_input(self):
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setFamily("Times New Roman")
        self.rec_input.setFont(font)
        self.rec_input.setTextMargins(5, 2, 0, 2)

    def add_rec(self):
        self.content_browser.setText('123')

    # TODO 删除
    # TODO 延迟
    def set_delay_label(self):
        self.delay_label.setText("设置延迟/s：")  # 创建一个QLabel并写入文字

    def set_delay_input(self):
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setFamily("Times New Roman")
        self.delay_input.setFont(font)
        self.delay_input.setTextMargins(5, 2, 0, 2)
        self.delay_input.setText('30')

    # TODO 启动

    # TODO 停止

    # 警告信息
    @staticmethod
    def message_dialog(title, text):
        msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, title, text)
        msg_box.exec_()


if __name__ == '__main__':
    gl_db = ''
    gl_host = ''
    gl_pwd = ''
    gl_user = ''
    gl_port = 0
    # 创建 QApplication 类的实例
    logapp = QApplication(sys.argv)
    # 创建窗体
    mainWindow = Six()
    # # 关联login
    # ui = Ui_widget()
    # ui.setupUi(mainWindow)
    # 打开窗体
    mainWindow.show()
    # 退出窗体
    sys.exit(logapp.exec_())
